package answer.controller;

import answer.mapper.TagMapper;
import answer.pojo.*;
import answer.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import commons.pojo.*;
import commons.vo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AnswerController {

//    @Autowired
//    private ItemSearchIdService itemSearchIdService;
    @Autowired
    private AnswerService answerService;
    @Autowired
    private SpecialService specialService;
    @Autowired
    private WeekService weekService;
    @Autowired
    private WrongService wrongService;
    @GetMapping ("/dayAnswer")
    public LivegoodsResult dayAnswer(){
        return answerService.answerSearch(5);
    }

    @GetMapping("/challengeAnswer")
    public LivegoodsResult challengeAnswer(){
        return answerService.everyAnswer();
    }

    @GetMapping("/week")
    public LivegoodsResult week(){ return weekService.WeekSearch();}

    @PostMapping("/weekAnswer")
    public LivegoodsResult weekAnswer(@RequestBody UserId userId){return weekService.WeekAnswer(userId.getUser(),userId.getId());}

    @GetMapping("/special")
    public LivegoodsResult special(){return specialService.SpecialSearch();}

    @PostMapping("/specialAnswer")
    public LivegoodsResult specialAnswer(@RequestBody NameN nameN) {return specialService.SpecialAnswer(nameN.getName(),nameN.getN());}

    @PostMapping("/searchWrong")
    public LivegoodsResult searchWrong(@RequestBody Idd user){
        return wrongService.getWrong(user.getId());
    }

    @PostMapping("/addWrong")
    public LivegoodsResult addWrong(@RequestBody Wrong wrong){
        return wrongService.AddWrong(wrong);
    }

    @PostMapping("/moveWrong")
    public LivegoodsResult moveWrong(@RequestBody UserId userId){
        return wrongService.WrongMove(userId.getUser(),userId.getId());
    }

    @Autowired(required = false)
    private ConsumerService consumerService;
    @Autowired
    private LikeService likeService;
    @Autowired
    private CollectionService collectionService;
    @Autowired
    private DetailsService detailsService;
    @Autowired
    private LogService logService;

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping ("/like")
    public LivegoodsResult selectLike(@RequestBody UserId id) {
        List<Love> list = likeService.selectLike(id.getUser());
        ArrayList<Object> items = new ArrayList<>();
        if(id.getId()==1){
            for (Love collection : list){
                if (collection.getCategory() == 1){
                    Item like = consumerService.getLike(collection.getLove());
                    items.add(like);
                }
            }
        }else {
            for (Love collection : list){
                if (collection.getCategory() == 2){
                    Shipin likee = consumerService.likee(collection.getLove());
                    items.add(likee);
                }
            }
        }
//        ArrayList<Item> items = new ArrayList<>();
//        for (Love collection : list) {
//            HttpHeaders header = new HttpHeaders();
//            header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
////请求参数 param 设置header之后等同于 http://xxx/xxx?password=xxxxx
//            MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
//            paramMap.add("id", collection.getLove());
//            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, header);
//            String s = restTemplate.postForObject(PATH_URL, httpEntity, String.class);
//            items.add(s);
//        }
        return LivegoodsResult.ok(items);
    }

    @PostMapping("/saveLike")
    public LivegoodsResult saveLike(@RequestBody Love like) {
        return likeService.saveLike(like);
    }

    @PostMapping("/isLike")
    public LivegoodsResult isLike(@RequestBody UserIdTemp userId) {
        return likeService.isLike(userId.getUser(),userId.getId(), userId.getTemp());
    }

    @PostMapping("/deleteLike")
    public LivegoodsResult deleteLike(@RequestBody NameItemTemp nameItem){
        return likeService.deleteLike(nameItem.getName(), nameItem.getItem(),nameItem.getTemp());
    }

    @PostMapping("/collection")
    public LivegoodsResult selectCollection(@RequestBody UserId id) {
        List<Collection> list = collectionService.selectCollection(id.getUser());
        ArrayList<Object> items = new ArrayList<>();
        if(id.getId()==1){
            for (Collection collection : list){
                if (collection.getCategory() == 1){
                    Item like = consumerService.getLike(collection.getCollection());
                    items.add(like);
                }
            }
        }else {
            for (Collection collection : list){
                if (collection.getCategory() == 2){
                    Shipin likee = consumerService.likee(collection.getCollection());
                    items.add(likee);
                }
            }
        }
//        ArrayList<String> items = new ArrayList<>();
//        for (Collection collection : list) {
//            HttpHeaders header = new HttpHeaders();
//            header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
////请求参数 param 设置header之后等同于 http://xxx/xxx?password=xxxxx
//            MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
//            paramMap.add("id", collection.getCollection());
//            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(paramMap, header);
//            String s = restTemplate.postForObject(PATH_URL, httpEntity, String.class);
//            items.add(s);
//        }
        return LivegoodsResult.ok(items);
    }

    @PostMapping("/saveCollection")
    public LivegoodsResult saveCollection(@RequestBody Collection collection) {
        return collectionService.saveCollection(collection);
    }

    @PostMapping("/deleteCollection")
    public LivegoodsResult deleteCollection(@RequestBody NameItemTemp nameItem){
        return collectionService.deleteCollection(nameItem.getName(), nameItem.getItem(),nameItem.getTemp());
    }
    @PostMapping("/log")
    public LivegoodsResult log(@RequestBody  Log log) {
        return logService.saveLog(log);
    }

    @PostMapping("/details")
    public LivegoodsResult selectDetails(@RequestBody Idd idd) {
        Details details = detailsService.searchDetail(idd.getId());
        return LivegoodsResult.ok(details);
    }

    @PostMapping("/setDetails")
    public LivegoodsResult setDetails(@RequestBody Details details) {
        return detailsService.setDetails(details);
    }

    @Autowired
    private AnsService ansService;

    @PostMapping("/ans")
    public LivegoodsResult ans(@RequestBody Idd idd){return ansService.selectAns(idd.getId());}

    @PostMapping("/updateAns")
    public LivegoodsResult updateAns(@RequestBody Ans ans){return ansService.updateAns(ans);}

    @Autowired
    private TagMapper tagMapper;

    @PostMapping("/tag")
    public LivegoodsResult tag(@RequestBody Str str){
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        tagQueryWrapper.eq("tag", str.getTag());
        Tag tag = tagMapper.selectOne(tagQueryWrapper);
        return LivegoodsResult.ok(tag.getId());
    }
}
